<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

/**
* This controller is used for JAEmail feature of the component
*/
class JAJobBoardControllerJAmanagefields extends JAJobBoardController {
/**
     * Constructor
     */
    function __construct( )
    {
    	
        parent::__construct(  );  
        checkPermission_Administrator('managefield');     
        // Register Extra tasks
        $this->registerTask( 'add',        'edit' );
        $this->registerTask( 'apply',    'save' );
        $this->registerTask( 'drop',    'remove' );
        $this->registerTask( 'unrequired',    'required' );
		$this->registerTask( 'orderup',    'orderdown' );
		$this->registerTask( 'unpublish', 	'publish');
		$this->registerTask( 'notsystem', 	'system');
    }
    
    /**
    * Display current jamanagefields of the component to administrator
    * 
    */
    function display(){
    	$temp = $this->getModel('jaconfiguration');
    	$jbconfig = $temp->getItems();
		$jbconfig = new JParameter($jbconfig->data);
		JRequest::setVar('config', $jbconfig);  
		$model = &$this->getModel('jacodes');
		JRequest::setVar( 'modeljacodes', $model );
		
    	switch($this->getTask())
		{
			case 'add'     :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'edit', false );
				JRequest::setVar( 'layout', 'form' );
				// Checkout the jaconfig
				$model = $this->getModel('jamanagefields');
				$model->checkout();
			} break;
			case 'edit':
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'edit', true );
				JRequest::setVar( 'layout', 'form' );
				// Checkout the jaconfig
				$model = $this->getModel('jamanagefields');
				$model->checkout();
			} break;
		}
		
		parent::display();
    }
    
    /**
    * Cancel current operation
    * 
    */
    function cancel(){
    	global $option;
        $this->setRedirect("index.php?option=$option&view=jamanagefields");
    }   
    /**
    * Remove a jamanagefields row
    * 
    */   
    function remove(){
    	global $option;
		
    	// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $model =& $this->getModel('jamanagefields');
        if(($n = $model->remove()) == 0){
             parent::display();
		}
		else 
		{
			$this->setMessage(JText::_('Successfully removed item'));
			$this->setRedirect("index.php?option=$option&view=jamanagefields");
		}
	}
	
	
	/**
	* Save categories record
	*/
	function save(){
		global $option;
		// Check for request forgeries
		//JRequest::checkToken() or jexit( 'Invalid Token' );
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		$cache = & JFactory::getCache($option);
		$cache->clean();

		$model	=& $this->getModel('jamanagefields');
		$post	= JRequest::get('post');
		
		$post['field_comment'] = JRequest::getVar( 'field_comment', '', 'post', 'string', JREQUEST_ALLOWRAW );
		
		$model->setState( 'request', $post );
		
		$msg = '';
		if ( $id = $model->store() ) {
			if ((isset($cid[0]))&&($cid[0]!=0))
				$msg = JText::_( 'Successfully updated field' );
			else 
				$msg = JText::_( 'Successfully created field, now you can set field order.' );
		} else {
			$msg =  JText::_( 'Error found! Fail to save field');
		}
		
		switch ( $this->_task ) {
			case 'apply':
				$this->setRedirect( "index.php?option=$option&view=jamanagefields&pretask=apply&task=edit&cid[]=$id" , $msg );
				break;

			case 'save':
			default:
				$this->setRedirect( "index.php?option=$option&view=jamanagefields", $msg );
				break;
		}
	}	
	
	
    
    /**
    * Move down the order of field list item
    * 
    */
    function orderdown(){
    	global $option;
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $cid    = JRequest::getVar( 'cid', array(), 'post', 'array' );
        JArrayHelper::toInteger($cid);
        
        $task = JRequest::getVar('task', 'orderdown');

        if (isset($cid[0]) && $cid[0]) {
            $id = $cid[0];
        } else {
            $this->setRedirect( "index.php?option=$option&view=jamanagefields", JText::_('No Items Selected') );
            return false;
        }

        $model =& $this->getModel( 'jamanagefields' );
        $movement = $task == 'orderdown' ? 1 : -1;

        if($model->orderItem($id, $movement)){
            $msg = $movement > 0 ? JText::_( 'Item Moved Down' ) : JText::_( 'Item Moved Up' );
        }
        
        $this->setRedirect( "index.php?option=$option&view=jamanagefields", $msg );
    }
    
    /**
    * Save the item(s) order
    */
    function saveorder()
    {
    	global $option;
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $cid    = JRequest::getVar( 'cid', array(), 'post', 'array' );
        JArrayHelper::toInteger($cid);

        $model =& $this->getModel( 'jamanagefields' );
        $msg = '';
        if ($model->setOrder($cid)) {
            $msg = JText::_( 'New ordering saved' );
        } else {
        	JError::raiseWarning(1, $model->getError());            
        }
        $this->setRedirect(  "index.php?option=$option&view=jamanagefields", $msg );
    }
    
    /**
	* Update database table
	* 
	*/
	function updateTable(){
		global $mainframe, $option;
		
		// JUtility::check('form.token') or jexit('Invalid token');
		$v = JRequest::getVar('view', '');
		$model = & $this->getModel('jamanagefields');
		$view  = & $this->getView($v, 'html');
	    $view->setModel($model, true);
	    
		$step = JRequest::getVar('step', 1);
		
		if($step == 1){ // view summary
			JRequest::setVar('updtable', true);
			$view->setLayout('updtable');
			JRequest::setVar('step', $step);
	    	$view->updtable();
		} else if($step == 2){ // do update table
			$msg = '';
			$field = $model->getItem();
			if($field->old_field_name=='' && $field->old_field_type=='' && $field->old_table_name==''){
				$db = JFactory::getDBO();
				$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
				JArrayHelper::toInteger($cid, array(0));
				$sql = "Update #__ja_form_fields SET field_state='chg' WHERE id={$cid[0]}";
				$db->setQuery($sql);
				if($db->query()){
					$msg = JText::_( 'Successfully updated table' ); 
				}
				else{
					JError::raiseWarning(1, JText::_('Error Updating the table'). "<br/> Message: ".$db->getErrorMsg() );
				}
			}
			else{
			
			 	$table = JRequest::getVar('filter_table');				
				if($model->updateTable($table)){
			 		$msg = JText::_( 'Successfully updated table' ); 
				} else {
					JError::raiseWarning(1, JText::_('Error Updating the table'));
				}
			}
			
			$this->setRedirect( 'index.php?option='. $option ."&view=$v", $msg );
		}
	}
	
	/**
	* change Is_Published status
	*/
	/**
	 * publish or unpublish list item 
	 * @return void
	 **/
	function publish(){
		global  $option;
		if($this->getTask()=='unpublish'){
			$publish = 0;
		}
		else{
			$publish = 1;
		}
		
		$model = $this->getModel('jamanagefields');
				
		if(!$model->dopublish('published', $publish)){
        	return JError::raiseWarning(1, $model->getError(true));        	
        }
        
		$cache = & JFactory::getCache($option);
		$cache->clean();
		
		if( $publish )
			$this->setRedirect("index.php?option=$option&view=jamanagefields", "Published successfully!");
		else 
			$this->setRedirect("index.php?option=$option&view=jamanagefields", "UnPublished successfully!");
	}		
	
	function required($task = 'required'){
		global $option;
    	// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
        $task = JRequest::getVar('task', 'required');
        $model =& $this->getModel('jamanagefields');
        
        $is_required = $task == 'required' ? 0 : 1;
        
        if(!$model->dopublish('is_required', $is_required)){
        	return JError::raiseWarning(1, $model->getError(true));        	
        }
        
		$cache = & JFactory::getCache($option);
		$cache->clean();
				
		$this->setRedirect('index.php?option='. $option .'&view=jamanagefields');
	}
	  	
	function system($task = 'system'){
		global $option;
    	// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
        $task = JRequest::getVar('task', 'system');
        $model =& $this->getModel('jamanagefields');
        
        $is_required = $task == 'system' ? 0 : 1;
        
        if(!$model->dopublish('is_system', $is_required)){
        	return JError::raiseWarning(1, $model->getError(true));        	
        }
        
		$cache = & JFactory::getCache($option);
		$cache->clean();
				
		$this->setRedirect('index.php?option='. $option .'&view=jamanagefields');
	}	
	
	function reverse(){
		global $option;
		
		$msg = JText::_('Successfully reversed field.');
		$temp = $this->getModel('jamanagefields');
		if(!$temp->doreverse()){
			$msg = '';
		}
		$this->setRedirect('index.php?option='. $option .'&view=jamanagefields', $msg);
	}
}